package com.clouds.wms.pur;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.bean.Purme;
import com.clouds.wms.bean.Purmf;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmjService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.service.PurmeService;
import com.clouds.wms.service.PurmfService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;
import com.clouds.wms.util.SysParams;

@Controller
@RequestMapping("/purmf")
public class PurmfController {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="purmeService")
	private PurmeService purmeService;
	@Resource(name="purmfService")
	private PurmfService purmfService;
	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;
	@Resource(name="cmsmjService")
	private CmsmjService cmsmjService;
	@RequestMapping(value = "/purmfMain.xhtml")
	public String purmfMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String meaa = RequestHelper.getString(request, "meaa");
		String meab = RequestHelper.getString(request, "meab");
		Purme purme = purmeService.getPurme(meaa, meab);
		model.addAttribute("meaa", meaa);
		model.addAttribute("meab", meab);
		model.addAttribute("purme", purme);
//		model.addAttribute("allCmsmjList", cmsmjService.getCmsmjListByMjac(SysParams.DOCUMENT_CATEGORY_CGDD));
		return "pur/purmf";
	}
	@RequestMapping(value = "/purmfList.xhtml")
	public String purmfList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String meaa = RequestHelper.getString(request, "meaa");
		String meab = RequestHelper.getString(request, "meab");
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = purmfService.getPurmfList(meaa, meab, pageNumber, pageSize);
		List<Purmf> list = (List<Purmf>) pb.getData();
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 添加采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/addPurmf.xhtml", method = RequestMethod.POST)
	public String addPurme(HttpServletRequest request, HttpServletResponse response, Model model, Purmf purmf) throws SysException, IOException {
	
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmf.setMfac(purmfService.getMaxMfac());
//		purmf.setMfam(0);
		purmfService.savePurcb(purmf);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + purmf.getMfab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 更新采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/updatePurmf.xhtml", method = RequestMethod.POST)
	public String updatePurme(HttpServletRequest request, HttpServletResponse response, Model model, Purmf purmf) throws SysException, IOException {
	
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmfService.updatePurcb(purmf);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("更新了编号为[" + purmf.getMfab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delPurmf.xhtml")
	public String delPurmf(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mfaa = RequestHelper.getString(request, "mfaa");
		String mfab = RequestHelper.getString(request, "mfab");
		String mfac = RequestHelper.getString(request, "mfac");
		StringBuffer mfacs = new StringBuffer();
		for(String s : mfac.split(",")){
			mfacs.append("'").append(s).append("',");
		}
		mfacs = mfacs.delete(mfacs.length()-1, mfacs.length());
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmfService.delPurmf(mfaa, mfab, mfacs.toString());
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + mfaa+","+mfab+","+mfac + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delPurmfAll.xhtml")
	public String delPurmfAll(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mfaa = RequestHelper.getString(request, "mfaa");
		String mfab = RequestHelper.getString(request, "mfab");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmfService.delPurmfAll(mfaa, mfab);
		out.print("success");
		return null;
	}
	/**
	 * 变更采购单状态
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/auditPurmf.xhtml", method = RequestMethod.POST)
	public String auditPurmf(HttpServletRequest request, HttpServletResponse response, Model model, Purmf purmf) throws SysException, IOException {
		String mfaa = RequestHelper.getString(request, "meaa");
		String mfab = RequestHelper.getString(request, "meab");
		String status = RequestHelper.getString(request, "flag");
//		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmfService.changeStatus(mfaa, mfab, status);
		
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delPurmemf.xhtml", method = RequestMethod.POST)
	public String delPurmemf(HttpServletRequest request, HttpServletResponse response, Model model, Purmf purmf) throws SysException, IOException {
		String mfaa = RequestHelper.getString(request, "meaa");
		String mfab = RequestHelper.getString(request, "meab");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmfService.delPurmemf(mfaa, mfab);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + purmf.getMfab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
}
